package com.mes.restful.control;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import com.mes.common.framework.Exception.DubboProviderException;
import com.mes.common.framework.rest.impl.BaseRestServerInterfaceImpl;
import com.mes.common.framework.rest.view.JsonViewObject;
import com.mes.common.framework.rest.view.Page;
import com.mes.dubbo.consumer.ControlConsumer;
import com.mes.dubbo.interprovider.control.ISalseOrderProvider;
import com.mes.entity.control.SalseOrder;
import com.mes.utils.RestConstants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.Map;

/**
 * Created by leden on 2017/12/4.
 */

@Api(value = "销售订单", description = "销售订单")
@Path(RestConstants.RestPathPrefix.SALSEORDER)
public class SalseOrderRestServer extends BaseRestServerInterfaceImpl<SalseOrder> {
    @Override
    public ISalseOrderProvider getDubboBaseInterface(){
        return ControlConsumer.getSalseOrderProvider();
    }

    @POST
    @Path("/saveSalseOrder")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "保存销售订单", notes = "保存销售订单", response = JsonViewObject.class, produces = MediaType.APPLICATION_JSON, consumes = MediaType.APPLICATION_JSON)
    public JsonViewObject saveSalseOrder(SalseOrder salseOrder){
        JsonViewObject jsonView=new JsonViewObject();
        try {
            String id = this.getDubboBaseInterface().saveSalseOrder(salseOrder);
            jsonView.successPack(id);
            this.addOperationLog("保存成功",id,true);
        } catch (DubboProviderException e) {
            jsonView.failPack(e);
            this.addOperationLog("保存失败","",false);
        }
        return jsonView;

    }
    @POST
    @Path("/countOrder")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public JsonViewObject countOrder(Page page){
        String jsonStr = JSON.toJSONString(page);
        try {
            Map<String, Object> mapBean = Maps.newHashMap();
            if (page != null) {
                if (page.getCondition() != null && Map.class.isInstance(page.getCondition())) {
                    mapBean = (Map) page.getCondition();
                }
            }
            page = this.getDubboBaseInterface().countOrder(page, mapBean);
            jsonView.successPack(page);
            this.addOperationLog("统计", jsonStr, true);
        } catch (Exception e) {
            jsonView.failPack(e);
            this.addOperationLog("统计", jsonStr, false);
            log.error("SalseOrderRestServer countOrder is error,{jsonStr:" + jsonStr + "}", e);
        }
        return jsonView;

    }
    @GET
    @Path("/chargeBackOrder")
    @Produces(MediaType.APPLICATION_JSON)
    public JsonViewObject updateChargeBackOrder(@QueryParam("id")String id){
        JsonViewObject jsonView=new JsonViewObject();
        try {
            boolean flag= this.getDubboBaseInterface().updateChargeBackOrder(id);
            jsonView.successPack(flag);
            this.addOperationLog("退单成功","",true);
        } catch (DubboProviderException e) {
            jsonView.failPack(e);
            this.addOperationLog("退单失败","",false);
        }

        return jsonView;
    }

}
